package com.xea.action;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.xea.model.Others;
import com.xea.service.NewsService;
import com.xea.service.OthersService;
import com.xea.service.ServiceFactory;

public class OthersAction extends DispatcherServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1932405126946046616L;
	
	
	/**
	 * 查询其它
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void search(HttpServletRequest request,HttpServletResponse response) throws Exception{
		OthersService othersDao = ServiceFactory.getOthersService();
		request.setAttribute("others", othersDao.search(Integer.valueOf(request.getParameter("type"))));
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher("inner/admin/others/list.jsp");
		dispatcher.forward(request, response);	
	}
	
	/**
	 * 新增其它
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void add(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		OthersService othersDao = ServiceFactory.getOthersService();
		response.setCharacterEncoding("GBK"); 
        String content=request.getParameter("content");
        String type=request.getParameter("type");
        String pub=request.getParameter("pub");
        if(Integer.valueOf(pub)==1){
        	othersDao.notpub(Integer.valueOf(type));
        }
        othersDao.add(new Others(Integer.valueOf(type),Integer.valueOf(pub), content));
	}
	
	/**
	 * 查看其它详情
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException 
	 */
	public void findById(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		OthersService othersDao = ServiceFactory.getOthersService();
		String id=request.getParameter("id");
		request.setAttribute("others", othersDao.findById(Integer.valueOf(id)));
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher(request.getParameter("_url"));
		dispatcher.forward(request, response);
	}
	
	/**
	 * 查看其它详情
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException 
	 */
	public void detail(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		OthersService othersDao = ServiceFactory.getOthersService();
		String id=request.getParameter("id");
		request.setAttribute("others", othersDao.findById(Integer.valueOf(id)));
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher("inner/admin/others/detail.jsp");
		dispatcher.forward(request, response);
	}
	
	/**
	 * 删除其它
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void del(HttpServletRequest request,HttpServletResponse response) throws IOException{
		OthersService othersDao = ServiceFactory.getOthersService();
		String id=request.getParameter("id");
		othersDao.del(id);
	}
	
	/**
	 * 修改其它
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void update(HttpServletRequest request,HttpServletResponse response) throws IOException{
		OthersService othersDao = ServiceFactory.getOthersService();
		String id=request.getParameter("id");
		String content=request.getParameter("content");
        String pub=request.getParameter("pub");
		response.setCharacterEncoding("utf-8");
        Others others=othersDao.findById(Integer.valueOf(id));
        others.setContent(content);
        others.setPub(Integer.valueOf(pub));
        if(Integer.valueOf(pub)==1)
        	othersDao.notpub(Integer.valueOf(others.getType()));
        othersDao.update(others);
	}
	/**
	 * 公开其它
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void pub(HttpServletRequest request,HttpServletResponse response) throws IOException{
		OthersService othersDao = ServiceFactory.getOthersService();
		String id=request.getParameter("id");
		String pub=request.getParameter("pub");
		String type=request.getParameter("type");
		if(Integer.valueOf(pub)==1)
			othersDao.notpub(Integer.valueOf(type));
		othersDao.pub(id,Integer.valueOf(pub));
	}
	
	/**
	 * 查询首页公开
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void searchPub(HttpServletRequest request,HttpServletResponse response) throws Exception{
		OthersService othersDao = ServiceFactory.getOthersService();
		NewsService newsDao=ServiceFactory.getNewsService();
		request.setAttribute("introduction", othersDao.findByIdPub(1));
		request.setAttribute("culture", othersDao.findByIdPub(2));
		request.setAttribute("news", newsDao.searchTop());
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher("outer/main.jsp");
		dispatcher.forward(request, response);	
	}
	
	/**
	 * 查询公司简介 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void searchInt(HttpServletRequest request,HttpServletResponse response) throws Exception{
		OthersService othersDao = ServiceFactory.getOthersService();
		request.setAttribute("introduction", othersDao.findByIdPub(1));
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher("outer/others/introduction.jsp");
		dispatcher.forward(request, response);	
	}
	
	/**
	 * 查询公司文化
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void searchCul(HttpServletRequest request,HttpServletResponse response) throws Exception{
		OthersService othersDao = ServiceFactory.getOthersService();
		request.setAttribute("culture", othersDao.findByIdPub(2));
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher("outer/others/culture.jsp");
		dispatcher.forward(request, response);	
	}
	
}
